package com.dev.dream.libs.share;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.dev.dream.libs.adapter.MySimpleArrayAdapter;
import com.dev.dream.libs.mylib.R;

public class MyShare {

	public static Bitmap getBitmapFromAssets(Context mContext, String fileName) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(mContext.getAssets().open(
					fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	public static void hehe() {
	}

	public static void saveImage(Bitmap bitMapImg, String forder,
			String fileName) {
		File filename;
		try {
			String path = Environment.getExternalStorageDirectory().toString();

			new File(path + "/" + forder).mkdirs();
			filename = new File(path + "/" + forder + "/" + fileName + ".png");

			// MainActivity.getInstance().setUrl_avatar(
			// path + "/EXO Lockscreen/icon.jpg");

			FileOutputStream out = new FileOutputStream(filename);

			bitMapImg.compress(Bitmap.CompressFormat.PNG, 90, out);
			out.flush();
			out.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static String[] app = new String[] { "Facebook", "Gmail", "Twitter" };
	public static int[] icon = new int[] { R.drawable.ic_facebook,
			R.drawable.ic_gmail, R.drawable.ic_twitter };

//	public static void actionShare(final Context mContext,
//			final String filePath, final String message, final String subject) {
//
//		Dialog dialog = new Dialog(mContext);
//		dialog.setContentView(R.layout.popup_back);
//
//		dialog.setTitle("Share...");
//		dialog.setCancelable(true);
//
//		dialog.show();
//
//		ListView lv = (ListView) dialog.findViewById(R.id.lv_app_back);
//		lv.setAdapter(new MySimpleArrayAdapter(mContext, app, icon));
//		// lv.setFocusable(false);
//
//		lv.setOnItemClickListener(new OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> parent, View view,
//					int position, long id) {
//				// startAppAd.show();
//				// startAppAd.load();
//
//				switch (position) {
//				case 0:
//				     share(mContext, "facebook", filePath, message, subject);
//					//shareTextUrl(mContext, message, subject);
//					break;
//
//				case 1:
//					share(mContext, "gmail", filePath, message, subject);
//					break;
//				case 2:
//					share(mContext, "twitter", filePath, message, subject);
//					break;
//				}
//				// Intent i = new Intent(Intent.ACTION_VIEW, Uri
//				// .parse(appURL[position]));
//				// startActivity(i);
//			}
//		});
//
//	}

	public static void shareTextUrl(Context mContext, String message,
			String subject) {
		Intent share = new Intent(android.content.Intent.ACTION_SEND);
		share.setType("text/plain");
		share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

		// Add data to the intent, the receiving app will decide
		// what to do with it.
		share.putExtra(Intent.EXTRA_SUBJECT, subject);
		share.putExtra(Intent.EXTRA_TEXT, message);

		mContext.startActivity(Intent.createChooser(share, "Share link!"));
	}

	public static void share(Context mContext, String nameApp,
			String imagePath, String message, String subject) {
		try {
			List<Intent> targetedShareIntents = new ArrayList<Intent>();
			Intent share = new Intent(android.content.Intent.ACTION_SEND);
			share.setType("image/jpeg");
			List<ResolveInfo> resInfo = mContext.getPackageManager()
					.queryIntentActivities(share, 0);
			if (!resInfo.isEmpty()) {
				for (ResolveInfo info : resInfo) {
					Intent targetedShare = new Intent(
							android.content.Intent.ACTION_SEND);
					targetedShare.setType("image/jpeg"); // put here your mime
															// type
					if (info.activityInfo.packageName.toLowerCase().contains(
							nameApp)
							|| info.activityInfo.name.toLowerCase().contains(
									nameApp)) {
						targetedShare.putExtra(Intent.EXTRA_SUBJECT, subject);

						if (info.activityInfo.packageName.toLowerCase()
								.contains("facebook")
								|| info.activityInfo.name.toLowerCase()
										.contains("facebook")) {
							targetedShare.putExtra(Intent.EXTRA_TEXT,
									"http://www.codeofaninja.com");
						} else {
							targetedShare.putExtra(Intent.EXTRA_TEXT, message);
						}

						targetedShare.putExtra(Intent.EXTRA_STREAM,
								Uri.fromFile(new File(imagePath)));
						targetedShare.setPackage(info.activityInfo.packageName);
						targetedShareIntents.add(targetedShare);

					}

				}

				Intent chooserIntent = Intent.createChooser(
						targetedShareIntents.remove(0), "Select app to share");
				chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
						targetedShareIntents.toArray(new Parcelable[] {}));
				mContext.startActivity(chooserIntent);
			}
		}

		catch (Exception e) {
			Log.v("VM",
					"Exception while sending image on" + nameApp + " "
							+ e.getMessage());
		}
	}
	
	
	
	
}
